<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <!-- 搜索框 -->
      <el-row>
        <el-col :span="8">
          <el-input
            placeholder="请输入内容"
            v-model="searchValue"
            class="input-with-select"
            clearable
            @clear='clearInput'
          >
            <el-button slot="append" @click="searchGoods" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <el-col :span="4">
          <el-button type="primary" @click="addGoods">添加商品</el-button>
        </el-col>
      </el-row>
      <el-table
      :data="goodsList"
      style="width: 100%" border stripe>
      <el-table-column
        type="index"
        label="#"
        width="80">
      </el-table-column>
      <el-table-column
        prop="goods_name"
        label="商品名称"
        >
      </el-table-column>
      <el-table-column
        prop="goods_price"
        label="商品价格(元)"
        width="120">
      </el-table-column>
      <el-table-column
        prop="goods_weight"
        label="商品重量"
        width="80">
      </el-table-column>
      <el-table-column
        prop="add_time"
        label="创建时间"
        width="170">
        <template slot-scope="scope">
          {{scope.row.add_time | dateFormat}}
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="120">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit"></el-button>
          <el-button @click="deleteGoods(scope.row)" size="mini" type="danger" icon="el-icon-delete"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="goodsParams.pagenum"
      background
      :page-sizes="[5, 10, 15, 20]"
      :page-size="goodsParams.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  created(){
    this.getGoodsList()
  },
  data(){
    return {
      searchValue:'',
      goodsList:[],
      // 获取商品列表请求的参数
      goodsParams:{
        query:'',
        pagenum:1,
        pagesize:10
      },
      total:0,
      
    }
  },
  methods:{
    async getGoodsList(){
      const {data:res}=await this.$http.get('goods',{params:this.goodsParams})
      if(res.meta.status!==200){
        this.$message.error('获取商品列表失败')
      }
      else{
        this.goodsList=res.data.goods
        this.total=res.data.total
        console.log(this.goodsList);
      }

    },
    // 每页显示的个数改变时
    handleSizeChange(newsize){
      this.goodsParams.pagesize=newsize
      this.getGoodsList()
    },
    // 页码改变时
    handleCurrentChange(newpage){
      this.goodsParams.pagenum=newpage
      this.getGoodsList()
    },
    // 根据输入的模糊信息查找所有相关商品
    searchGoods(){
      this.goodsParams.query=this.searchValue
      this.getGoodsList()
    },
    clearInput(){
      this.goodsParams.query=''
      this.getGoodsList()
    },
    // 根据id删除商品
    deleteGoods(row){
      console.log(row);
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
         const {data:res}=await this.$http.delete('goods/'+row.goods_id)
         if(res.meta.status!==200){
           this.$message.error('删除失败')
         }
         else{
           this.$message.success('删除成功')
           this.getGoodsList()
         }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
    },
    // 添加商品按钮
    addGoods(){ 
      this.$router.push('/addgoods')
    }
  }
};
</script>

<style scoped>
.el-col-8{
  margin-right: 15px;
}

</style>